package com.example.myapplication.fragments.fragmentsview;

import android.graphics.Rect;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentHomeBinding;
import com.example.myapplication.fragments.fragmentsadapter.HomeRecyclerAdapter;
import com.example.myapplication.fragments.fragmentsmodel.Missing;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private FragmentHomeBinding binding;
    private HomeRecyclerAdapter adapter; //HomeFragment里面recyclerview的适配器

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home,container,false);

        binding.HomeRecyclerView.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext()));
        adapter = new HomeRecyclerAdapter(binding.getRoot().getContext());
        binding.HomeRecyclerView.addItemDecoration(new SpaceItemDecoration(30));
        binding.HomeRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new HomeRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(Missing missing) {
                Toast.makeText(getActivity(), missing.getName() + "--" + missing.getAge(), Toast.LENGTH_LONG).show();
            }
        });
        List<Missing> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Missing missing = new Missing("王德顺","58岁","2021-01-28 23:40","湖南省常德市临澧县","走失时穿着一个灰色袄子和一双解放鞋");
            list.add(missing);
        }
        adapter.addAll(list);

        return binding.getRoot();
    }

    class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        int mSpace;

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.bottom = mSpace;
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.top = mSpace;
            }
        }

        public SpaceItemDecoration(int space) {
            this.mSpace = space;
        }
    }
}